<template>
    <div class="login">
        <el-form ref="form" :model="form" label-width="80px" class="form">
            <el-form-item label="账号">
                <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="密码">
                <el-input v-model="form.pwd"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="onSubmit">立即登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    data() {
        return {
            form: {
                name: "admin",
                pwd: "admin",
            },
        };
    },
    methods: {
        onSubmit() {
            if (this.form.username == "" || this.form.pwd == "") {
                this.$message("账号密码不能为空");
                return;
            }
            this.$axios
                .post("http://ceshi5.dishait.cn/admin/login", {
                    username: this.form.name,
                    password: this.form.pwd,
                })
                .then((res) => {
                    console.log(res);
                    let date = new Date();
                    let time = date.getTime() / 1000; //过期时间为当前时间的一分钟后
                    let obj={
                        token: res.data.data.token,
                        time: time,
                    }
                    localStorage.setItem("token", JSON.stringify(obj));
                    localStorage.setItem("name", this.form.name);
                    this.$message({
                      type:'success',
                      message:'登录成功'
                    })
                    setTimeout(()=>{
                      this.$router.push('/index')
                    },1200)
                })
                .catch((err) => {
                    this.$message("账号或密码错误");
                });
        },
    },
};
</script>

<style lang="scss">
.login {
    width: 100%;
    height: 100%;
    overflow: hidden;
    .form {
        margin: 100px auto;
        width: 300px;
        height: 300px;
    }
}
</style>